package com.example.qa_back.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qa_back.entity.CollectQuestion;
import com.example.qa_back.mapper.BackCQMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author 董帅
 * @date 2021/3/7 - 12:18
 */
@Service
public class BackCQService {
    private final BackCQMapper backCQMapper;

    public BackCQService(BackCQMapper backCQMapper) {
        this.backCQMapper = backCQMapper;
    }


    /*
    * 功能：分页展示CQ
    * param：page，页数，最小为1；num，每页的个数
    * return：objectPage，每页内容
    * */
    public Page<CollectQuestion> getPage(Integer page, Integer num) {
        Page<CollectQuestion> objectPage = new Page<>(page, num);
        backCQMapper.selectPage(objectPage, null);
        return objectPage;
    }

    /*
    * 功能：单个删除CQ
    * param：id，CQ的id
    * return：0，删除失败；1，删除成功
    * */
    public Integer deleteCQ(Integer id) {
        int i = backCQMapper.deleteById(id);
        if (i == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    /*
    * 功能：批量删除CQ
    * param：ids，CQ的id
    * return：0，删除失败；1，删除成功
    * */
    public Integer deleteManyCQ(Integer[] ids) {
        int i = backCQMapper.deleteBatchIds(Arrays.asList(ids));
        if (i != 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /*
    * 功能：返回所有CQ
    * */
    public List<CollectQuestion> getAllCQ() {
        QueryWrapper<CollectQuestion> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("id");
        return backCQMapper.selectList(wrapper);
    }
}
